﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

public partial class User_SUserDefineShowColumn : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();
    UserDefineShowColumnDAO comm = new UserDefineShowColumnDAO();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        if (!IsPostBack)
        { WebAndGroup(Session["UserCode"].ToString()); }
    }

    #region 初始化绑定 WebAndGroup(string UserCode)
    protected void WebAndGroup(string UserCode)
    {
        //权限
        comHelp.GetRightUnite(this, new string[] { UserCode, Session["CoCode"].ToString(), "userDefineShowColumn" }, new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 3, 1 }, CommonModel.loginOut1);
        //绑定
        bind(Session["UserCode"].ToString());
    }
    
    // 绑定角色组
    protected void bind(string userCode)
    {
        DataTable[] dt = comm.GetRolesColumnGroupAndNotShowStr(userCode);
        ddlName.DataTextField = "RGName";
        ddlName.DataValueField = "RGCode";
        ddlName.DataSource = dt[0];
        ddlName.DataBind();

        
        if (dt.Length > 1 && dt[1].Rows.Count > 0)
        {
            hidColumnTypes.Value = dt[1].Rows[0]["ColumnTypes"].ToString();
            hidDefault.Value = hidColumnTypes.Value + "/";
        }

        InOutGroupBind(ddlName.SelectedValue.ToString(), userCode);
    }
    #endregion

    //分别绑定未在此组和在组的
    protected void InOutGroupBind(string GCode,string userCode)
    {
        //显示的
        DataTable[] dt = comm.GetGetInOutGroup_UserDefineShowColumn(userCode, GCode);
        lbxInGroup.DataSource = dt[0];
        lbxInGroup.DataValueField = "ColumnType";
        lbxInGroup.DataTextField = "ColumnName";
        lbxInGroup.DataBind();

        //不显示的
        lbxNotInGroup.DataSource = dt[1];
        lbxNotInGroup.DataValueField = "ColumnType";
        lbxNotInGroup.DataTextField = "ColumnName";
        lbxNotInGroup.DataBind();
    }

    //组动态绑定
    protected void ddlWebGroupName_TextChanged(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        InOutGroupBind(ddlName.SelectedValue.ToString(), Session["UserCode"].ToString());
    }

    #region 分组
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < lbxInGroup.Items.Count; i++)
        {
            if (lbxInGroup.SelectedIndex >= 0)
            {
                lbxNotInGroup.Items.Add(lbxInGroup.SelectedItem);
                hidColumnTypes.Value = hidColumnTypes.Value + lbxInGroup.SelectedItem.Value.ToString() + "/";
                lbxInGroup.Items.Remove(lbxInGroup.SelectedItem);
            }
        }
    }

    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        //int InNum = lbxWebInGroup.Items.Count;
        for (int i = 0; i < lbxNotInGroup.Items.Count; i++)
        {
            if (lbxNotInGroup.SelectedIndex >= 0)
            {
                lbxInGroup.Items.Add(lbxNotInGroup.SelectedItem);
                hidColumnTypes.Value = hidColumnTypes.Value.Replace(lbxNotInGroup.SelectedItem.Value.ToString(), "");
                lbxNotInGroup.Items.Remove(lbxNotInGroup.SelectedItem);
            }
        }
    }

    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {        
        //不在组里的页面数
        for (int i = 0; i < lbxInGroup.Items.Count; i++)
        {
            lbxNotInGroup.Items.Add(lbxInGroup.Items[i]);
            hidColumnTypes.Value = hidColumnTypes.Value + lbxInGroup.Items[i].Value.ToString() + "/";
        }
        lbxInGroup.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        for (int i = 0; i < lbxNotInGroup.Items.Count; i++)
        {
            lbxInGroup.Items.Add(lbxNotInGroup.Items[i]);
        }
        hidColumnTypes.Value = "";
        lbxNotInGroup.Items.Clear();
    }
    #endregion

    //保存
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);       

        if (!hidColumnTypes.Value.Equals(hidDefault.Value))
        {
            try
            {
                //当前用户
                string UserName = Session["UserCode"].ToString();
                comm.Add_Update_UserDefineShowColumn(UserName, hidColumnTypes.Value);
                CommonMethod.regAlertMessage("操作成功！", this);
                InOutGroupBind(ddlName.SelectedItem.Value.ToString(), UserName);
            }
            catch { }
        }
    }
    
    //查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        lbxInGroup.Items.Clear();

        //未在组里的
        string StrWNGSql = string.Format(" select RC.ColumnType,RC.ColumnName from B_Roles_Column RC join B_RolesGroupAndColumn RGC on RC.ColumnType=RGC.ColumnType "
            + "join M_UserDefineShowColumn USC on USC.ColumnTypes  like '%'+RC.ColumnType+'/%' "
            + "where USC.UserCode='{0}', and RGC.RGCode='{1}' and (RC.ColumnType like '%{2}%' or RC.ColumnName like '%{3}%')", 
            Session["UserCode"].ToString(), ddlName.SelectedValue.ToString(), txtCondition.Text.ToString(), txtCondition.Text.ToString());

        lbxInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxInGroup.DataValueField = "RGCode";
        lbxInGroup.DataTextField = "RGName";
        lbxInGroup.DataBind();
    }
}